package com.macro.mall.tiny.modules.oms.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.macro.mall.tiny.common.api.CommonResult;
import com.macro.mall.tiny.modules.oms.model.OmsMemberBalanceLog;
import com.macro.mall.tiny.modules.oms.service.OmsMemberBalanceLogService;
import com.macro.mall.tiny.modules.ums.model.UmsMember;
import com.macro.mall.tiny.security.util.MemberContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 用户余额变更表 前端控制器
 * </p>
 *
 * @author tyrone
 * @since 2024-10-31
 */
@RestController
@RequestMapping("/oms/omsMemberBalanceLog")
@Api(tags = "OmsMemberBalanceLogController")
@Tag(name = "OmsMemberBalanceLogController", description = "余额管理")
public class OmsMemberBalanceLogController {

    @Autowired
    private OmsMemberBalanceLogService omsMemberBalanceLogService;

    @ApiOperation("前台用户余额变更历史查询")
    @PostMapping("/list")
    public CommonResult<IPage<OmsMemberBalanceLog>> list(@RequestBody OmsMemberBalanceLog omsMemberBalanceLog) {
        UmsMember member = MemberContext.getMember();
        QueryWrapper<OmsMemberBalanceLog> query = new QueryWrapper<>();
        query.eq("open_id", member.getOpenId()).orderByDesc("create_time");
        Page<OmsMemberBalanceLog> page = omsMemberBalanceLogService.page(new Page<>(omsMemberBalanceLog.getPageNum(), omsMemberBalanceLog.getPageSize(), true), query);
        page.getRecords().forEach(log -> {
             log.setDescription(log.getDescription().replaceAll(" 获得收益：\\d+ 分", ""));
        });
        return CommonResult.success(page);
    }

    @ApiOperation("后台用户余额变更历史查询")
    @PostMapping("/page")
    public CommonResult<IPage<OmsMemberBalanceLog>> page(@RequestBody OmsMemberBalanceLog omsMemberBalanceLog) {
        QueryWrapper<OmsMemberBalanceLog> query = new QueryWrapper<>();
        if (omsMemberBalanceLog.getMemberId() != null) {
            query.eq("member_id", omsMemberBalanceLog.getMemberId());
        }
        if (omsMemberBalanceLog.getOpenId() != null) {
            query.eq("open_id", omsMemberBalanceLog.getOpenId());
        }
        if (omsMemberBalanceLog.getType() != null) {
            query.eq("type", omsMemberBalanceLog.getType());
        }
        if (omsMemberBalanceLog.getCreateTimeStart() != null) {
            query.ge("create_time", omsMemberBalanceLog.getCreateTimeStart());
        }
        if (omsMemberBalanceLog.getCreateTimeEnd() != null) {
            query.le("create_time", omsMemberBalanceLog.getCreateTimeEnd());
        }
        query.orderByDesc("create_time");
        Page<OmsMemberBalanceLog> page = omsMemberBalanceLogService.page(new Page<>(omsMemberBalanceLog.getPageNum(), omsMemberBalanceLog.getPageSize(), true), query);
        return CommonResult.success(page);
    }

    @ApiOperation("更新用户余额变更信息")
    @PostMapping("/update")
    public CommonResult<OmsMemberBalanceLog> update(@RequestBody OmsMemberBalanceLog omsMemberBalanceLog) {
        boolean success = omsMemberBalanceLogService.updateById(omsMemberBalanceLog);
        if (success) {
            return CommonResult.success(omsMemberBalanceLog);
        }
        return CommonResult.failed();
    }
}

